/*
 * progress_bar_in_console.c
 *
 *  Created on: 2012-12-5
 *      Author: wangijia
 *  Implement progress in console using "\r"
 */

#include <stdio.h>

void show_progress(unsigned long total, unsigned long cur)
{
       static unsigned long p = 0;
       static const unsigned char w[] = "\\/-";
       unsigned long val;

       val = (cur * 100) / total;
       if(++p == 3)  p = 0;
       printf("\r %4d%c%c", val, '%', w[p]);
}

int main(void)
{
    int i;
       int j;
       int k;
       for (i=0; i <= 100; i++)
       {
              for (j=0; j <10000; j++)
                     for (k=0; k <10000; k++)
                                   ;
           show_progress(100, i);
              fflush(stdout);
       }
       printf("\n");
       return 0;
}
